package com.ruoyi.project.cms.document.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.dept.domain.MDept;
import com.ruoyi.project.cms.document.domain.ZzDocument;
import com.ruoyi.project.cms.document.service.DocumentService;
import com.ruoyi.project.cms.role.domain.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司:
 * 作者:胖虎
 * 类名:DocumentController
 * 描述:
 * 时间:2020-05-21
 */

@Controller
@RequestMapping("/cms/document")
public class DocumentController   extends BaseController {


    private String prefix = "cms/document";


    @Autowired
    private DocumentService  documentService;

    @GetMapping()
    public String document() {
        return    prefix + "/document";
    }

    /**
     * 文档列表的查询
     * @param document
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZzDocument document)
    {

        startPage();
        List<ZzDocument> list = documentService.selectDocumentList(document);

        return getDataTable(list);
    }

    /**
     * 新增审批文档的跳转
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 添加文档
     * @param document
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZzDocument document)
    {


        return toAjax(documentService.insertDocument(document));

    }


    /**
     * 修改页面的跳转 ,同时伴随着数据
     */
    @GetMapping("/edit/{docId}")
    public String edit(@PathVariable("docId") Integer  docId, ModelMap mmap)
    {
        ZzDocument document = documentService.selectDeptById(docId);


        mmap.put("document", document);
        return   prefix + "/edit";
    }


    /**
     * 修改
     * @param document
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave( ZzDocument document)
    {


        return toAjax(documentService.updateDocument(document));
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String  ids)
    {
        try
        {
            return  toAjax(documentService.deleteDocumentByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


}
